<?php

namespace App\Utils;

use App\Models\Admin;

/**
 * 系统菜单配置
 */
class MenuUtil
{
    /**
     * 后台系统菜单配置
     */
    public static function adminMenu()
    {
        $menu = array(
            [
                "title" => '安全管理',
                "id" => "1",
                "child" => [
                    [
                        "title" => "管理员管理",
                        "id" => "1-1",
                        "child" => [
                            ["title" => "管理员列表", "id" => "1-1-1"]
                        ]
                    ],
                    [
                        "title" => "角色管理",
                        "id" => "1-2",
                        "child" => [
                            ["title" => "角色列表", "id" => "1-2-1"],
                        ]
                    ],

                ]
            ],

        );
        return $menu;
    }

    /**
     * 根据管理员权限生成可操作的菜单数组
     * @param Admin $admin 管理员对象
     * @return array
     */
    public static function adminCanMenus($admin)
    {
        $allMenus = self::adminMenu();
        $result = array();
        foreach ($allMenus as $tab) {
            $tabs = array();
            $tabs['id'] = $tab['id'];
            $tabs['title'] = $tab['title'];
            $groups = array();
            foreach ($tab['child'] as $menus) {
                $groupItem = array();
                $groupItem['id'] = $menus['id'];
                $groupItem['title'] = $menus['title'];
                $childs = [];
                foreach ($menus['child'] as $item) {
                    if ($admin->rolesCan($item['id'])) {
                        $childItem = [];
                        $childItem['id'] = $item['id'];
                        $childItem['title'] = $item['title'];
                        $childs[] = $childItem;
                    }
                }
                if (!empty($childs)) {
                    $groupItem['child'] = $childs;
                    $groups[] = $groupItem;
                }
            }
            if (!empty($groups)) {   //只有TAB下有菜单项才加入菜单
                $tabs['child'] = $groups;
                $result[] = $tabs;
            }
        }
        return $result;
    }
}